﻿namespace WordCloudClient.ViewModels
{
    public class WinEditFilterViewModel : WinBaseVM
    {
        
        
        public WinEditFilterViewModel(SystemService systemService, WinEditHelper winEditHelper) : base(systemService, winEditHelper)
        {
        }
        private FilterWordsEntity _filterWordsEntity = new();
        public FilterWordsEntity FilterWordsEntity
        {
            get{ return _filterWordsEntity; }
            set
            {
                _filterWordsEntity = value;
                RaisePropertyChanged(nameof(FilterWordsEntity));
            }
        }

        public override void ExecuteSave()
        {
            if (!CommonHelper.AttributeVerify(FilterWordsEntity))
            {
                return;
            }
            bool res;
            string errorMsg = string.Empty;
            res = Title.Contains("新增")
                ? _systemService.AddFilterWord(FilterWordsEntity, ref errorMsg)
                : _systemService.UpdateFilterWord(FilterWordsEntity, ref errorMsg);
            if (res)
            {
                RaiseRequestClose(new DialogResult(ButtonResult.OK));
            }
            else
            {
                MessageTips.ErrorMessage($"操作失败！{errorMsg}");
                return;
            }
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            string operationType = parameters.GetValue<string>("OperationType");
            if (operationType == "新增")
            {
                Title = "新增单词";
                FilterWordsEntity = new();
            }
            else if (operationType == "编辑")
            {
                Title = "编辑单词";
                FilterWordsEntity = parameters.GetValue<FilterWordsEntity>("FristEntity");
            }
        }
    }
}
